import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter} from '@/router'
import router from '@/router'
import {activeRouter,anyRouter,constantRoutes} from '@/router/router'
import _ from 'lodash'

function fillterRouter(newRouter,router){
  const res = []
  newRouter.forEach(item => {
    if (router.includes(item.meta.name)) {
      console.log(item,'112');
      if (item.children && item.children.length>0) {
        item.children=fillterRouter(item.children,router)
        console.log(item.children,'221');
      }
      res.push(item)
    }
  });
  return res
}


// import { resetRouter } from '@/router'
// import { jingtai ,no,constantRoutes} from '@/router/router'
// import router from "@/router/index"
// import _ from "lodash"

// function filtrateAllSyncRoutes(a,b){
//   const shuzhu = []
//   a.forEach((item)=>{
//     console.log(b.includes(item.meta.name))
//     if(b.includes(item.meta.name)){
//       if(item.children&&item.children.length>0){
//         item.children = filtrateAllSyncRoutes(item.children,b)
//       }
//       shuzhu.push(item)
//     }
//   })
//   return shuzhu
// }

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    routes:[],
    buttons:[],
    roles:[],
    newRouters:[]
    // buttons:'',
    // routes:'',
    // menuRoutes:[]

  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  set_routes:(state,routes)=>{
    state.routes = routes
  },
  set_buttons:(state,buttons)=>{
    state.buttons = buttons
  },
  set_roles:(state,roles)=>{
    state.roles = roles
  },
  set_newRouters:(state,newRouters)=>{
    state.newRouters = newRouters
  },
  // SET_BUTTONS:(state,buttons)=>{
  //   state.buttons=buttons
  // },
  // SET_ROUTES:(state,routes)=>{
  //   state.routes=routes
  // },
  // SET_MENUROURES:(state,menuRoutes)=>{
  //   state.menuRoutes=menuRoutes
  // }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        // console.log(response)
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response
        // console.log("111222")

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar ,routes,buttons,roles} = data

        // console.log(routes,buttons,roles);

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('set_routes', routes)
        commit('set_buttons', buttons)
        commit('set_roles', roles)


        const newRouter = fillterRouter(_.cloneDeep(activeRouter),routes)


        commit('set_newRouters', [...constantRoutes,...newRouter,])

        router.addRoutes(newRouter)
        router.addRoutes(anyRouter)

        // const { name, avatar ,routes,buttons} = data

        // commit('SET_NAME', name)
        // commit('SET_AVATAR', avatar)
        // commit('SET_BUTTONS',buttons)
        // commit('SET_ROUTES',routes)
        // const xin= filtrateAllSyncRoutes(_.cloneDeep(jingtai),this.state.user.routes)
        // console.log(xin,"22222222222222222")
        // commit('SET_MENUROURES',[...constantRoutes,...xin])
        // console.log("33333333333333333")
        // router.addRoutes([...xin,no])
        resolve(data)
        console.log("999999999999999")
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    console.log("推出了")
    return new Promise((resolve, reject) => {
      logout().then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

